<!DOCTYPE html>
<html lang='zh-cn'>
<head>
    <meta charset='UTF-8'>
    <meta name='description' content=''>
    <meta name='keywords' content=''>
    <meta name='viewport' content='width=device-width, initial-scale=1.0,user-scalable=no'>
    <title>Document</title>
    <style>
        body{
            font-size: 30px;
        }
        span{
            float: left;
            width: 30px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div>
        <span>☆</span>
        <span>☆</span>
        <span>☆</span>
        <span>☆</span>
        <span>☆</span>
    </div>
    <p></p>
    <script>

        var spans = document.querySelectorAll('span');
        var p = document.querySelector('p');

        var cur = -1;
        
        for(var i = 0; i < spans.length; i++){

            spans[i].index = i;

            spans[i].onmouseenter = function(){
                
                empty();
                
                full(this.index);
            }

            spans[i].onmouseleave = function(){

                empty();

                full(cur);

                info(cur);
            }

            spans[i].onclick = function(){

                cur = this.index;

                info(cur);
            }
        }

        function empty(){

            for(var i = 0; i < spans.length; i++){

                spans[i].innerHTML = '☆';

            }
        }

        function full(len){

            for(var i = 0; i <= len; i++){

                spans[i].innerHTML = '★';


            }

            info(len);
        }
        
        function info(n){
            p.innerHTML = n > -1 ? n + 1 + '星' : '';
        }
    </script>
</body>
</html>